package edu.java.texbooks.tutorial.test05.collections.queue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

public class TestQueue {

	private static final int MAX_NUMBER = 40;
	private static final int TIME_INTERVAL = 500;

	static String number01 = "a-1";
	static String number02 = "b-2";
	static String number03 = "c-3";
	static String number04 = "d-4";
	static String number05 = "e-5";
	static String number06 = "f-6";
	static String number09 = "g-9";

	public static void main(String[] args) {

		// test 1
		// timeQueue();

		// test 2
		// stringNumberQueue();

		// test 3
		testPriorityQueue();

	}

	public static void timeQueue() {
		Queue<Integer> myQueue = new LinkedList<Integer>();

		for (int i = MAX_NUMBER; i >= 0; i--) {
			myQueue.add(i);
		}

		while (!myQueue.isEmpty()) {
			Integer tempInteger = myQueue.element();
			int tempInt = tempInteger.intValue();
			if ((tempInt % 10) == 0) {
				System.out.println("------- Stage " + (tempInt / 10) + "-----");
			}

			System.out.println("elem: " + myQueue.remove());

			try {
				Thread.sleep(TIME_INTERVAL);
			} catch (InterruptedException ie) {
				System.out.println("Interrupted exception during"
						+ " execution of sleep() method");
				ie.printStackTrace();
			}
		}
	}

	public static void stringNumberQueue() {
		String[] stringNumbers = { number01, number05, number04, number06,
				number02, number03 };
		List<String> myList = new ArrayList<String>(Arrays
				.asList(stringNumbers));

		Queue<String> myQueue = new LinkedList<String>(myList);

		System.out.println(myQueue);

		myQueue.add(number06);
		System.out.println(myQueue);
		myQueue.clear();
		System.out.println(myQueue);
		myQueue.addAll(myList);
		System.out.println(myQueue);
		System.out.println("contains " + number03 + ": "
				+ myQueue.contains(number03));
		System.out.println(myQueue.size());

		myQueue.clear();
		myQueue.add(number01);
		myQueue.offer(number01);
		myQueue.add(number03);
		myQueue.add(number05);
		System.out.println(myQueue);
		System.out.println("poll/remove element: " + myQueue.poll());
		System.out.println(myQueue);
		System.out.println("peek() element: " + myQueue.peek());
		System.out.println("element() element: " + myQueue.element());
		System.out.println(myQueue);

		System.out.println("\n------------------\n");
		myQueue.clear();
		myQueue.add(number01);
		myQueue.add(number02);
		myQueue.add(number03);
		System.out.println(myQueue);
		System.out.println("element(): " + myQueue.element());
		System.out.println("remove element from queue: " + myQueue.remove());
		System.out.println(myQueue);

		System.out.println("\n------------------\n");
		myQueue.clear();
		myQueue.offer(number01);
		myQueue.offer(number02);
		myQueue.offer(number03);
		System.out.println(myQueue);
		System.out.println("Peek(): " + myQueue.peek());
		System.out.println("poll() element: " + myQueue.poll());
		System.out.println(myQueue);
		System.out.println("Is Empty: " + myQueue.isEmpty());

	}

	public static void testPriorityQueue() {

		Queue<String> priorityQueue = new PriorityQueue<String>();
		priorityQueue.add(number03);
		priorityQueue.offer(number06);
		priorityQueue.offer(number09);
		priorityQueue.offer(number05);
		System.out.println(priorityQueue);

		while (!priorityQueue.isEmpty()) {
			System.out.println(priorityQueue.poll());
		}
	}
}
